//
//  CZWelcomeViewController.swift
//  Weibo27
//
//  Created by apple on 16/4/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
class CZWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = bgImageView
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setHeadImageViewAnim()
    }
    
    private func setHeadImageViewAnim(){
        // 更新我们的headImageView 约束
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        // 01 -  动画执行时长
        // 02 - delay 延迟时长
        // 03 - Damping 阻尼 (0 - 1) 阻尼越小 弹性效果越强
        // 04 - Velocity 起始速度
        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            // 需要刷新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                       NSNotificationCenter.defaultCenter().postNotificationName(SwitchDefaultVcNSNotification, object: "welcome")
                })
                
        }
        
    }
    
    // MARK: - 设置视图
    private func setupUI(){

        // 01 - 添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        // 02 - 添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
        
    }
    
    // MARK: - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // 用户头像
    private lazy var headImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "avatar_default_big"))
        img.layer.cornerRadius = 85 / 2
        img.layer.borderColor = ThemeColor.CGColor
        img.layer.borderWidth = 2
        img.clipsToBounds = true 
        img.sd_setImageWithURL(NSURL(string: CZUserAccountTools.sharedTools.userAccount?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        return img
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let lab =  UILabel(textColor: UIColor.darkGrayColor(), textFont: 17)
        lab.textAlignment = .Center
        lab.text = "欢迎回来"
        lab.alpha = 0
        return lab
    }()
}
